//index.js
//获取应用实例
var app = getApp()
var req = require('../../utils/reqUtils.js')
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false , // loading
    userInfo: {},
    swiperCurrent: 0,  
    selectCurrent:0,
    categories: [],
    activeCategoryId: 0,
    goods:[],
    scrollTop:"0",
    loadingMoreHidden:true
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  //事件处理函数
  swiperchange: function(e) {
      //console.log(e.detail.current)
       this.setData({  
        swiperCurrent: e.detail.current  
    })  
  },
  toDetailsTap:function(e){
    wx.navigateTo({ 
      url:"/pages/course-reserve/course-reserve?card_number="+e.currentTarget.dataset.card_number
    })
  },
  tapBanner: function(e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function(e) {
     this.setData({  
        selectCurrent: e.index  
    })  
  },
  scroll: function (e) {
    //  console.log(e) ;
    var that = this,scrollTop=that.data.scrollTop;
    that.setData({
      scrollTop:e.detail.scrollTop
    })
    // console.log('e.detail.scrollTop:'+e.detail.scrollTop) ;
    // console.log('scrollTop:'+scrollTop)
  },
  onLoad: function () {
    console.log('onLoad');
    var that = this;
    wx.setNavigationBarTitle({
      title: "全部课程"
    });
    /*
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
    */
    req.post('/banner/index', {}, 1).then(res => {
      console.log(res.data);
      this.setData({
        banners:res.data.bannerList
      })
    });

    // req.post('/get_user_appointment_list/index', {}, 1).then(res => {
    req.post('/get_user_course_list/index', {'u_id':wx.getStorageSync('userInfo')['u_id']}, 1).then(res => {
      console.log(res.data); 
      var categories = [{coach_id:0, coach_name:"全部"}];
      for(var i=0;i<res.data.user_course_list.length;i++){
        categories.push(res.data.user_course_list[i]); 
      }
      that.setData({
        categories:categories,
        activeCategoryId:0
      });
      that.getGoodsList(0);
    });    

  },
  getGoodsList: function (categoryId) {
    // if (categoryId == 0) {
    //   categoryId = "";
    // }
    console.log(categoryId)
    var that = this;
    req.post('/get_user_course_list/index', {'id':wx.getStorageSync('userInfo')['u_id']}, 1).then(res => {
      console.log(res.data);
      that.setData({
        goods:[],
        loadingMoreHidden:true 
      });
      var goods = [];   
      console.log(res.data.user_course_list);
      if (res.data.status != 1 || res.data.user_course_list.length == 0) {
        console.log(res.data.user_course_list);
        that.setData({
          loadingMoreHidden:false,
        }); 
        return;
      } 
      console.log(1); 
      var date = new Date();

      for(var i=0;i<res.data.user_course_list.length;i++){
        res.data.user_course_list[i].count = res.data.user_course_list[i].count+res.data.user_course_list[i].gift_um
       
        let dateObj = {};
        let date = new Date(res.data.user_course_list[i].join_card_date);
        date.setDate(date.getDate() + res.data.user_course_list[i].validity_date);
        dateObj.year = date.getFullYear();
        dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1);
        dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
        dateObj.hours = (date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours());
        dateObj.minutes = (date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date.getMinutes());
        res.data.user_course_list[i].end_time = dateObj.year+'-'+dateObj.month+'-'+dateObj.day
        // dateObj.week = show_day[day];
        // let end_date = new Date(res.data.user_course_list[i].join_card_date).getTime() + (res.data.user_course_list[i].validity_date)*24*3600*1000;
        let time = new Date();
        console.log(Math.floor((date - time)/(24*3600*1000))); 
        res.data.user_course_list[i].surplus = Math.floor((date - time)/(24*3600*1000));
        goods.push(res.data.user_course_list[i]);
      } 
      that.setData({
        goods:goods,
      });
    });
   
  }
})
